package com.jvyou.house.service.impl;

import com.jvyou.common.core.domain.AjaxResult;
import com.jvyou.common.utils.DateUtils;
import com.jvyou.common.utils.StringUtils;
import com.jvyou.house.domain.House;
import com.jvyou.house.domain.HouseImage;
import com.jvyou.house.mapper.HouseMapper;
import com.jvyou.house.service.IHouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 房源Service业务层处理
 *
 * @author Jvyou
 * @date 2024-09-16
 */
@Service
public class HouseServiceImpl implements IHouseService {
    @Autowired
    private HouseMapper houseMapper;

    /**
     * 查询房源
     *
     * @param id 房源主键
     * @return 房源
     */
    @Override
    public House selectHouseById(Long id) {
        return houseMapper.selectHouseById(id);
    }

    /**
     * 查询房源列表
     *
     * @param house 房源
     * @return 房源
     */
    @Override
    public List<House> selectHouseList(House house) {
        return houseMapper.selectHouseList(house);
    }

    /**
     * 新增房源
     *
     * @param house 房源
     * @return 结果
     */
    @Transactional
    @Override
    public int insertHouse(House house) {
        house.setCreateTime(DateUtils.getNowDate());
        int rows = houseMapper.insertHouse(house);
        insertHouseImage(house);
        return rows;
    }

    /**
     * 修改房源
     *
     * @param house 房源
     * @return 结果
     */
    @Transactional
    @Override
    public int updateHouse(House house) {
        house.setUpdateTime(DateUtils.getNowDate());
        houseMapper.deleteHouseImageByHouseId(house.getId());
        insertHouseImage(house);
        return houseMapper.updateHouse(house);
    }

    /**
     * 批量删除房源
     *
     * @param ids 需要删除的房源主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteHouseByIds(Long[] ids) {
        houseMapper.deleteHouseImageByHouseIds(ids);
        return houseMapper.deleteHouseByIds(ids);
    }

    /**
     * 删除房源信息
     *
     * @param id 房源主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteHouseById(Long id) {
        houseMapper.deleteHouseImageByHouseId(id);
        return houseMapper.deleteHouseById(id);
    }

    @Override
    public String getImages(Long id) {
        List<HouseImage> images = houseMapper.getImages(id);
        return images.stream().map(HouseImage::getImageUrl).collect(Collectors.joining(","));
    }

    @Override
    public AjaxResult saveImage(Long id, List<HouseImage> houseImageus) {

        if (houseImageus == null) {
            return AjaxResult.warn("图片为空");
        }
        // 获取所有的图片
        List<HouseImage> images = houseMapper.getImages(id);
        List<HouseImage> finalList = new ArrayList<>();
        // 判断有没有重复的
        houseImageus.forEach(houseImage -> {
            if (images.stream().noneMatch(image -> image.getImageUrl().equals(houseImage.getImageUrl()))) {
                finalList.add(houseImage);
            }
        });
        finalList.forEach(houseImage -> houseImage.setHouseId(id));
        houseMapper.batchHouseImage(finalList);
        return AjaxResult.success();
    }

    /**
     * 新增房屋图片信息
     *
     * @param house 房源对象
     */
    public void insertHouseImage(House house) {
        List<HouseImage> houseImageList = house.getHouseImageList();
        Long id = house.getId();
        if (StringUtils.isNotNull(houseImageList)) {
            List<HouseImage> list = new ArrayList<HouseImage>();
            for (HouseImage houseImage : houseImageList) {
                houseImage.setHouseId(id);
                list.add(houseImage);
            }
            if (list.size() > 0) {
                houseMapper.batchHouseImage(list);
            }
        }
    }
}
